package com.redis.jedistest;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.exceptions.JedisConnectionException;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author lzy
 * @date 2020-10-13 下午2:53
 */
public class RedisPool {
    private final JedisPool pool;

    public RedisPool() {
        this.pool = new JedisPool();
    }

    public static void main(String[] args) {
        RedisPool redisPool = new RedisPool();
        redisPool.execute(jedis -> {
            // do something
            jedis.set("k1", "v1");
        });
    }

    public void execute(CallWithJedis caller) {
        try (final Jedis jedis = pool.getResource()) {
            try {
                caller.call(jedis);
            } catch (JedisConnectionException ex) {
                // 重试一次
                caller.call(jedis);
            } finally {
                jedis.close();
            }
        }
    }
}
